﻿using FineUI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using ZHDJ.BranchCenter.BLL;
using ZHDJ.Data.BLL;
using ZHDJ.Data.Models.Center;
using ZHDJ.WebPage;

namespace ZHDJ.BranchCenter.Module.PartyVolunteer
{
    public partial class PartyVolunteerActivityRecordEdit : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }

        #endregion

        #region 页面属性
        public string EditorContent
        {
            get
            {
                string id = GetQueryIntValue("id");
                if (!string.IsNullOrEmpty(id))
                {
                    PartyVolunteerActivitie article = DB.PartyVolunteerActivities.Find(id);
                    return article.Summary;
                }
                else
                {
                    return string.Empty;
                }
            }
        }

        public string EditorRecord
        {
            get
            {
                string id = GetQueryIntValue("id");
                if (!string.IsNullOrEmpty(id))
                {
                    PartyVolunteerActivitie article = DB.PartyVolunteerActivities.Find(id);
                    return article.Record;
                }
                else
                {
                    return string.Empty;
                }
            }
        }
        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        private void BindData()
        {
            ControlBusiness.ControlBindDataDictionary(ddlThematic, "ZHDJ.BranchActivity.Thematic");
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartyVolunteerActivitie activity = DB.PartyVolunteerActivities.Find(id);
                if (activity == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                txtTitle.Text = activity.Title;
                ddlCategory.SelectedValue = activity.Category;
                ddlThematic.SelectedValue = activity.Thematic;
                dpBeginTime.Text = Convert.ToDateTime(activity.BeginTime).ToString("yyyy-MM-dd");
                dpEndTime.Text = Convert.ToDateTime(activity.EndTime).ToString("yyyy-MM-dd");
                if (!string.IsNullOrEmpty(activity.FileID))
                {
                    hlAttachment.NavigateUrl = UpFileBusiness.GetFileUrl(activity.FileID);
                    hlAttachment.Text = UpFileBusiness.GetFileName(activity.FileID);
                }
                else
                {
                    hlAttachment.Text = "暂无附件";
                }
                txtAddress.Text = activity.Address;
                ddlIsShowHome.SelectedValue = activity.IsShowHome ? "1" : "0";
                txtCreateBy.Text = this.CurrentUser.UserName;
                txtServiceTime.Text = activity.ServiceTime.ToString();
                cbIsTop.Checked = activity.Flags == 1;
                hdImageID.Text = activity.ImageID;

                IQueryable<PartyVolunteerActivitieMember> q = DB.PartyVolunteerActivitieMembers.Where(t => t.ActivitieID == id);

                if (q.Count() > 0)
                {
                    StringBuilder sbName = new StringBuilder();

                    foreach (PartyVolunteerActivitieMember item in q)
                    {
                        sbName.Append(ControlBusiness.GetMembersPickerText(item.LoginID) + ",");
                    }

                    txtMembers.Text = sbName.ToString().TrimEnd(',');

                    DataTable dtMember = PartyVolunteerActivitieMember.GetGridData(id);
                    cblActualMember.DataSource = dtMember;
                    cblActualMember.DataBind();

                    foreach (CheckItem item in cblActualMember.Items)
                    {
                        DataRow[] dataRow = dtMember.Select("LoginID='" + item.Value + "' and Status=2");
                        item.Selected = dataRow.Length > 0;
                    }
                }
            }
        }
        #endregion

        #region Events

        private void SaveData(bool isEnd)
        {
            string id = GetQueryIntValue("id");
            string _id = id;
            List<string> joinedMember = new List<string>();
            if (id != "")
            {
                //更新
                PartyVolunteerActivitie activity = DB.PartyVolunteerActivities.Find(id);
                if (activity == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                DataTable dtMember = PartyVolunteerActivitieMember.GetGridData(id);

                foreach (CheckItem item in cblActualMember.Items)
                {
                    if (item.Selected)
                    {
                        PartyVolunteerActivitieMember.UpdateMemberStatus(id, item.Value);
                        joinedMember.Add(item.Value);
                    }
                }
                activity.Title = txtTitle.Text.Trim();
                activity.Record = this.Request.Form["recordValue"];
                activity.ServiceTime = Convert.ToInt32(txtServiceTime.Text.Trim());
                activity.Flags = cbIsTop.Checked ? 1 : 0;
                if (!(bool)activity.IsEnd)
                {
                    activity.IsEnd = isEnd;
                }
                if (!string.IsNullOrEmpty(hdImageID.Text))
                {
                    activity.ImageID = hdImageID.Text;
                }
                activity.ModifyTime = DateTime.Now;
                activity.ModifyBy = CurrentUser.LoginID;

                if (DB.SaveChanges() > 0 && isEnd)
                {
                    //增加积分
                    PartyScoreHelper.Add(() => PartyScoreHelper.AddVolunteerActivity(joinedMember, activity.BranchID, CurrentUser.LoginID, activity.ID, Convert.ToDateTime(activity.BeginTime).ToString("yyyy-MM-dd")));
                }
            }
        }

        protected void rblOperate_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.rblOperate.SelectedValue.Equals("0"))
            {
                foreach (CheckItem item in cblActualMember.Items)
                {
                    item.Selected = true;
                }
            }
            else if (this.rblOperate.SelectedValue.Equals("1"))
            {
                foreach (CheckItem item in cblActualMember.Items)
                {
                    item.Selected = false;
                }
            }
            else if (this.rblOperate.SelectedValue.Equals("2"))
            {
                foreach (CheckItem item in cblActualMember.Items)
                {
                    if (item.Selected)
                    {
                        item.Selected = false;
                    }
                    else
                    {
                        item.Selected = true;
                    }
                }
            }
        }

        protected void filePhoto_FileSelected(object sender, EventArgs e)
        {
            var rs = UpFileBusiness.Upload(filePhoto, CurrentUser.LoginID);
            if (rs.IsSuccess)
            {
                hdImageID.Text = rs.FileID;
                HyperLink1.ImageID = rs.FileID;
            }
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData(false);

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void btnEndActivity_Click(object sender, EventArgs e)
        {
            SaveData(true);

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
    }
}